/*
 *  Copyright (C) 2010 fabien
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.dao.impl;

import com.boc.botv.common.Database;
import com.boc.botv.dao.CategoryDao;
import com.boc.botv.model.Category;
import java.util.List;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Fabien Renaud
 */
@Repository("categoryDao")
public class CategoryDaoImpl implements CategoryDao {

  public List<Category> getChildsOf(int id) {
    return (List<Category>) Database.getSession().createQuery("from Category as c where c.parentId=" + id).list();
  }

  public void saveCategory(Category c) {
    Database.getSession().save(c);
  }

  public Category getCategory(Integer id) {
    return (Category) Database.getSession().load(Category.class, id);
  }

  public Category getCategory(String name) {
    Query q = Database.getSession().createQuery("from Category as c where c.name=:name");
    q.setString("name", name);
    List<Category> list = q.list();
    if (list.isEmpty()) {
      return null;
    }
    return list.get(0);
  }

  public List<Category> getCategory() {
    return (List<Category>) Database.getSession().createQuery("from Category").list();
  }
}
